package quick.portal.activity;

import java.io.IOException;

import com.alibaba.fastjson.JSON;
import com.squareup.okhttp.Request;

import quick.common.AppContext;
import quick.common.AppManager;
import quick.common.UIHelper;
import quick.communication.AsyncHttpResponseHandler;
import quick.portal.R;
import quick.portal.data.APIResult;
import quick.portal.data.UserInfo;
import quick.portal.service.UserInfoService;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


public class LoginActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        
        AppManager.getAppManager().addActivity(this);

        this.setButtonClose();
        this.setButtonLogin();
    }
    

	@Override
	protected void onResume() {
		super.onResume();
		
		EditText txtUser = (EditText)this.findViewById(R.id.Username);
    	EditText txtPwd= (EditText)this.findViewById(R.id.password);
    	
    	txtUser.setText("");
    	txtPwd.setText("");
    	
    	txtUser.requestFocus();
	}
    
    
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        
        // 结束Activity从堆栈中移除
        AppManager.getAppManager().finishActivity(this);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    
    
    private void setButtonClose()
    {
    	this.findViewById(R.id.btnClose).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppManager.getAppManager().AppExit(LoginActivity.this);
            }
        });
    }
    
    private void setButtonLogin()
    {
    	this.findViewById(R.id.btnSure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
    }
    
    private void login()
    {
    	final ProgressDialog pd = ProgressDialog.show(this, "", "登陆中...");
    	
    	EditText txtUser = (EditText)this.findViewById(R.id.Username);
    	EditText txtPwd= (EditText)this.findViewById(R.id.password);
    	
    	if(TextUtils.isEmpty(txtUser.getText()))
		{
    		txtUser.setError("请填写用户名");
    		txtUser.requestFocus();
    		pd.dismiss();
    		return;
		}
    	
    	if(TextUtils.isEmpty(txtPwd.getText()))
		{
    		txtPwd.setError("请填写密码");
    		txtPwd.requestFocus();
    		pd.dismiss();
    		return;
		}
    	
    	String username = txtUser.getText().toString();
    	String password = txtPwd.getText().toString();

    	new UserInfoService().login(username,password, new AsyncHttpResponseHandler()
        {
        	@Override
            public void onSuccess(String body) {

        		pd.dismiss();
        		        		
        		try
        		{
        			APIResult result = JSON.parseObject(body, APIResult.class);
        			
        			if(result.getReturnCode()<0)
    	        	{
    	        		Toast.makeText(LoginActivity.this,result.getMessage() , Toast.LENGTH_LONG).show();
    	        		return;
    	        	}
    	        	
    	        	UserInfo user = JSON.parseObject(result.getBody(), UserInfo.class);    	        	
    	        	AppContext.getInstance().setUserInfo(user.getName(), result.getMessage());
    				UIHelper.showActivity(LoginActivity.this, MainActivity.class);
        		}
	        	catch(Exception ex)
	        	{
	        		Toast.makeText(LoginActivity.this,"登陆失败:非法的返回内容", Toast.LENGTH_LONG).show();
	        	}

        	}
        	
        	@Override
            public void onFailure(Request request, IOException e) {
        		pd.dismiss();
        		Toast.makeText(LoginActivity.this,"登陆失败：\r\n" + e.getMessage() , Toast.LENGTH_LONG).show();
            }
        });
    	
    }

}
